package com.jlz.weibo;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class FileUtils {

	//描述和文件名的对应内容，在程序运行时一次性加载到内存中
	public static Map<String,Map<String,String>>picDesMap = new HashMap<String,Map<String,String>>();
	public static Map<String,String>configMap = new HashMap<String,String>();
	
	private static String filePath = "";

	//本系统的配置文件路径
	private static String configFile = "config.xml";
	//图片名字和文字说明的对应文件路径
	private static String picDesMapFile = "picdesmap.xml";

	private static String allConfig = "//config";
	private static String fileName = "filename";
	
	public static String msg = "msg";
	public static String car = "car";
	public static String url = "url";
	public static String title = "title";
	
	
	static{
		filePath = filePath + FileUtils.class.getResource("").getPath().substring(1);
		
		//程序运行时加载到内存中
		getPicDesMap();
		getConfigMap();
	}

	@SuppressWarnings({ "unchecked", "null" })
	public static void getConfigMap(){
		Document document = getDocument(filePath+ configFile);
		if (document == null) {
			System.out.println(configFile + "文件没有找到或者放的位置错误，请检查！");
		}
		Element root = document.getRootElement();
		
		List<Element> list = root.selectNodes("/" + root.getName() + "/*");
		if (list != null || list.size()>0) {
			for (int i = 0; i < list.size(); i++) {
				Element config = list.get(i);
//				System.out.println(config.getName());
//				System.out.println(config.getText());
				configMap.put(config.getName(), config.getText());
			}
		}
	}
	
	@SuppressWarnings({ "unchecked", "null" })
	public static void getPicDesMap(){
		Document document = getDocument(filePath+ picDesMapFile);
		if (document == null) {
			System.out.println(picDesMapFile + "文件没有找到或者放的位置错误，请检查！");
		}
		Element root = document.getRootElement();
		List<Element> list = root.selectNodes(allConfig);
		if (list != null || list.size()>0) {
			for (int i = 0; i < list.size(); i++) {
				Element config = list.get(i);
				Map<String,String>replacedMap = new HashMap<String,String>();
				replacedMap.put(msg, config.selectSingleNode(msg).getText());
				replacedMap.put(car, config.selectSingleNode(car).getText());
				replacedMap.put(url, config.selectSingleNode(url).getText());
				replacedMap.put(title, config.selectSingleNode(title).getText());
				picDesMap.put(config.selectSingleNode(fileName).getText(), replacedMap);
				System.out.println(config.selectSingleNode(fileName).getText());
				System.out.println(config.selectSingleNode(msg).getText());
			}
		}
	}
	
	private static Document getDocument(String filePath){
		Document document = null;
        try {
            SAXReader reader = new SAXReader();
            File file = new File(filePath);
            
            if (file.exists()) {
                document = reader.read(file);
            }else {
				return null;
			}
        }catch (Exception e) {
			e.printStackTrace();
		}
		
		return document;
	}
	
	public static void main(String[] args) {
//		getPicDesMap();
	}

}
